--
-- Author: yawei.zhu
-- Date: 2018-05-08 11:06:55
--
-- GoldTaskPanel
--

local GoldTaskRender = import(".renders.GoldTaskRender")

local ClassRef = app.mvc.createPanelClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TaskCircleNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:initCircleTaskNodes()
end

function ClassRef:initCircleTaskNodes()
    local topNode = self._ui["Panel/top"]
    self._circleRootPanel = topNode:getChildByName("Panel")
    self._circleMapNameNode = self._circleRootPanel:getChildByName("name_map")
    self._circleLoadingBarBG = self._circleRootPanel:getChildByName("bar")
    self._circleLoadingBar = self._circleLoadingBarBG:getChildByName("LoadingBar")
    self._circleFameExpNode = self._circleRootPanel:getChildByName("num")
    self._circleMapBgNode = self._circleRootPanel:getChildByName("img")

    self._circleListView = self._ui["Panel/ListView"]
    self._circleListView:removeAllItems()
    for i = 1, 3 do
        local render = GoldTaskRender.create()
        render:setIndex(i)
        self._circleListView:pushBackCustomItem(render)
    end
    self._circleListView:refreshView()

    self._circleImgTips = self._ui["Panel/img_tips"]
    self._circleImgTips:setVisible(false)
end

function ClassRef:refresh()
    for i = 1, 3 do
        local render = self._circleListView:getItem(i - 1)
        render:onBtnClick(self._onOperate)
    end
end

function ClassRef:updateCircleTaskProgress(fameSubId)

    local progressText = self._ui["Panel/num"]
    local taskInfo = executeFunc(self._onOperate, "getBountyTaskInfo")
    local progressStr = table.concat( { taskInfo:getFinishedNum(), "/", taskInfo:getMaxNum() } )
    progressText:setString(L("task_gold_desc1") .. progressStr)

    -- local fameSubId = executeFunc(self._onOperate, "getSelFameSubId")
    local percent,percentStr = executeFunc(self._onOperate, "getBountyTaskProgressBySubkey", fameSubId)

    self._circleLoadingBar:setPercent(percent)
    self._circleFameExpNode:setString(percentStr)

    local fameSc = executeFunc(self._onOperate, "getFameSubKeyById", fameSubId)
    local fameSName = (fameSc and L(fameSc.name) or fameSubId.." not found")
    self._circleMapNameNode:setString(fameSName)

    local taskBannerName = (fameSC and fameSC.taskBanner) or ""
    local taskBannerPath = GD:getImagePath(Res.renownBgPath, taskBannerName)
    if taskBannerPath then
        self._circleMapBgNode:setVisible(true)
        self._circleMapBgNode:loadTexture(taskBannerPath)
    else
        self._circleMapBgNode:setVisible(false)
    end
end

function ClassRef:updateCircleTask(fameSubId)
    if fameSubId == nil then return end

    fameSubId = tonumber(fameSubId)
    if fameSubId <= 0 then return end

    executeFunc(self._onOperate, "setSelFameSubId", fameSubId)
    self:updateCircleTaskProgress(fameSubId)

    if not self._cricleDesAction then
        self._cricleDesAction = cc.CSLoader:createTimeline(Res.TaskCircleItemNode)
        self:runAction(self._cricleDesAction)
        self._cricleDesAction:gotoFrameAndPause(0)
    end

    local btnMore = self._circleRootPanel:getChildByName("Button_more")
    btnMore:addClickEventListener(function(sender)
        if self._cricleDesAction then
            if not self._openStatus then
                self:openCircleDesPanel()
            else
                self:closeCircleDesPanel()
            end
        end
    end)

    -- 赏金任务是否已经全部完成
    if self._onOperate("isBountyTaskFinished") then
        self._circleImgTips:setVisible(true)
        self._circleListView:setVisible(false)
        return 
    end

    local tasks = executeFunc(self._onOperate, "getBountyTasks")[tonumber(fameSubId)]
    if not tasks then return end

    for i = 1, 3 do
        local item = self._circleListView:getItem(i - 1)
        if item then item:setData(tasks[i]) end
    end
end

-- 播放赏金任务刷新成功动画
function ClassRef:playRefreshAni(index)

    local panel = self._ui["Panel"]
    panel:removeChildByName( "_refreshAnimationTemp" )
    local amiNodeTemp = cc.CSLoader:createNode( Res.TaskRefreshAmi )
    amiNodeTemp:setName( "_refreshAnimationTemp" )
    amiNodeTemp:setPosition( panel:getChildByName( "ani_" .. index ):getPosition() )
    panel:addChild( amiNodeTemp )
    amiNodeTemp:getTimeline():gotoFrameAndPlay( 0, false )

    local actions = {}
    table.insert( actions, cc.DelayTime:create( amiNodeTemp:getTimeline():getDuration()/60 ) )
    table.insert( actions, cc.CallFunc:create( function() amiNodeTemp:removeFromParent() end ) )
    table.insert( actions, cc.RemoveSelf:create() )
    amiNodeTemp:runAction( cc.Sequence:create( actions ) )
end

function ClassRef:openCircleDesPanel()
    if not self._cricleDesAction then return end
    if self._openStatus then return end

    self:updateCircleTaskSpreadMenu()
    self._openStatus = true
    self._cricleDesAction:play("in", false)
    self._cricleDesAction:clearLastFrameCallFunc()
    self._cricleDesAction:setLastFrameCallFunc(function()
        self._cricleDesAction:clearLastFrameCallFunc()
    end)
end

function ClassRef:closeCircleDesPanel()
    if not self._cricleDesAction then return end
    if not self._openStatus then return end

    self._openStatus = false
    self._cricleDesAction:play("out", false)
    self._cricleDesAction:clearLastFrameCallFunc()
    self._cricleDesAction:setLastFrameCallFunc(function()
        self._cricleDesAction:clearLastFrameCallFunc()
    end)
end

function ClassRef:updateCircleTaskSpreadMenu()
    local fameSubId = executeFunc(self._onOperate, "getSelFameSubId")
    local famesC = executeFunc(self._onOperate, "getBountyTaskFameBySubkey", fameSubId)
    local moreNode = self._circleRootPanel:getChildByName("more")
    local panelRoot = moreNode:getChildByName("Panel")
    panelRoot:setTouchEnabled(true)
    display.nodeRemoveForNotHit(panelRoot, function()
        self:closeCircleDesPanel()
    end)
    for i = 1, 4 do
        local name = "bar_" .. i
        local barRoot = panelRoot:getChildByName(name)
        local fameC = famesC[i]
        if fameC then
            barRoot:setVisible(true)
            local loadingBar = barRoot:getChildByName("LoadingBar")
            local Text = barRoot:getChildByName("Text")
            Text:setString(fameC.fameStr)
            loadingBar:setPercent(fameC.famePercent)
        else
            barRoot:setVisible(false)
        end
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
